var pageInitVar = {
	listButton: {
		add: {
			text: "新增",
			paramNames: "code",
			iconCls : 'icon-add',
			handler: function() {
			},
			success: function(bool) {
			}
		}
	},
	editButton: {
		save: {
			text: "保存",
			paramNames: "code",
			iconCls : 'icon-save',
			handler: function() {
			},
			success: function() {
			}
		},
		reset: {
			text: "重置",
			paramNames: "code",
			iconCls : 'icon-reset',
			handler: function() {
			},
			success: function() {
			}
		},
		cancel: {
			text: "取消",
			iconCls : 'icon-cancel',
			handler: function() {
			},
			success: function() {
			}
		}
	},
	listOper: {
		modify: {
			text: "修改",
			paramNames: "code, node, index",
			handler: function(gridData, grid, index, data) {
			},
			success: function() {
			}
		},
		remove: {
			text: "删除",
			paramNames: "code, node, index",
			handler: function(gridData, grid, index, data) {
			},
			success: function() {
			}
		}
	},
	clicks: {
		click: {
			text: "单击",
			paramNames: "code, node, index",
			handler: function(index, data) {
			},
			success: function() {
			}
		},
		dblClick: {
			text: "双击",
			paramNames: "code, node, index",
			handler: function(index, data) {
			},
			success: function() {
			}
		}
	}
}
function changeType(value) {
	$("#pageFunsDiv").html("");
	$("#pageOpersDiv").html("");
	var bottons = {};
	//不同页面类型，显示不同的按钮功能
	$("#btnPanel").show();
	if (value == "list") {
		bottons = pageInitVar.listButton;
		var opers = pageInitVar.listOper;
		//显示操作信息
		for (var oper in opers) {
			addPageOperToPanel(opers[oper], oper);
		}
		
		$("#operPanel").show();
		$("#paginationDiv").show();
		$("#hideQueryDiv").show();
		$("#clickPanel").show();
		$("#pageSizeDiv").show();
	} else if (value == "tree") {
		bottons = pageInitVar.listButton;
		var opers = pageInitVar.listOper;
		//显示操作信息
		for (var oper in opers) {
			addPageOperToPanel(opers[oper], oper);
		}
		$("#operPanel").hide();
		$("#clickPanel").show();
		$("#paginationDiv").hide();
		$("#hideQueryDiv").hide();
		$("#pageSizeDiv").hide();
		$("#pagination").val(0);
	} else if (value == "tabs") {
		$("#paginationDiv").hide();
		$("#hideQueryDiv").hide();
		$("#pagination").val(0);
		$("#operPanel").hide();
		$("#clickPanel").hide();
		$("#pageSizeDiv").hide();
		$("#btnPanel").hide();
	} else {
		bottons = pageInitVar.editButton;
		$("#paginationDiv").hide();
		$("#hideQueryDiv").hide();
		$("#pagination").val(0);
		$("#operPanel").hide();
		$("#clickPanel").hide();
		$("#pageSizeDiv").hide();
	}
	if (value == "edit") {
		$("#pageSizeDiv").show();
		$("#pageSizeTitle").html("行列数:");
	} else {
		$("#pageSizeTitle").html("分页表格数量:");
	}
	for (var fun in bottons) {
		addPageButtonToPanel(bottons[fun], fun);
	}
}
function initPageButtonToPanel(type, json) {
	eval("json=" + json);
	for (var btn in json) {
		if (type == "list" || type == "tree") {
			pageInitVar.listButton[btn] = json[btn];
		} else {
			pageInitVar.editButton[btn] = json[btn];
		}
	}
	pageInitVar.defaultButton = json;
}
function initPageOperToPanel(type, json) {
	eval("json=" + json);
	for (var oper in json) {
		if (type == "list") {
			pageInitVar.listOper[oper] = json[oper];
		}
	}
	pageInitVar.defaultOper = json;
}
function addPageButtonToPanel(json, value) {
	var checked = json.checked != "false" ? "checked='checked'" : "";
	var html = '<div><input name="pageFuns" type="checkbox" value="' + value + '" title="' + json.text + '" ' + checked + ' />' +
		'<a href="javascript: void(0)" onclick="showFuns(\'' + value + '\');return false;">' + json.text + '&nbsp;</a></div>';
	$("#pageFunsDiv").append($(html));
}
var click_btn_id = null;
function showFuns(value) {
	var type = $("#type").val();
	var btn = {};
	if (type == "list" || type == "tree") {
		btn = pageInitVar.listButton[value];
	} else {
		btn = pageInitVar.editButton[value];
	}
	btnType = 1;
	click_btn_id = value;
	$("#btnName").val(btn.text);
	$("#btnContext").val((btn.handler + "").replace(/\	/g, ""));
	if (btn.success) {
		$("#successFun").val((btn.success + "").replace(/\	/g, ""));
	} else {
		$("#successFun").val("");
	}
	$('#buttonWindow').window('open');
}
function addPageOperToPanel(json, value) {
	var checked = json.checked != "false" ? "checked='checked'" : "";
	var html = '<div><input name="pageOper" type="checkbox" value="' + value + '" title="' + json.text + '" ' + checked + '/>' +
		'<a href="javascript: void(0)" onclick="showOper(\'' + value + '\');return false;">' + json.text + '&nbsp;</a></div>';
	$("#pageOpersDiv").append($(html));
}
function showOper(value) {
	btnType = 2;
	click_btn_id = value;
	$("#btnName").val(pageInitVar.listOper[value].text);
	$("#btnContext").val((pageInitVar.listOper[value].handler + "").replace(/\	/g, ""));
	
	if (pageInitVar.listOper[value].success) {
		$("#successFun").val((pageInitVar.listOper[value].success + "").replace(/\	/g, ""));
	} else {
		$("#successFun").val("");
	}
	$('#buttonWindow').window('open');
}
function showClick(value) {
	btnType = 3;
	click_btn_id = value;
	$("#btnName").val(pageInitVar.clicks[value].text);
	$("#btnContext").val((pageInitVar.clicks[value].handler + "").replace(/\	/g, ""));
	if (pageInitVar.clicks[value].success) {
		$("#successFun").val((pageInitVar.clicks[value].success + "").replace(/\	/g, ""));
	} else {
		$("#successFun").val("");
	}
	$('#buttonWindow').window('open');
}
function initClickToPanel(type, json) {
	try {
		eval("json=" + json);
		var list = document.getElementsByName("pageClick");
		for (var i = 0;i < list.length;i ++) {
			if (json[list[i].value]) {
				list[i].checked = (json[list[i].value] != "false");
				pageInitVar.clicks[list[i].value] = json[list[i].value];
			}
		}
	} catch (e) {
	}
}
//获取按钮信息
function getButtons() {
	var type = $("#type").val();
	var list = document.getElementsByName("pageFuns");
	var buttons = [];
	for (var i = 0;i < list.length;i ++) {
		var json = {};
		if (type == "list" || type == "tree") {
			json = pageInitVar.listButton[list[i].value];
		} else {
			json = pageInitVar.editButton[list[i].value];
		}
		json.checked = list[i].checked;
		var array = [];
		for (var key in json) {
			array.push("\"" + key + "\": " + (typeof json[key] == "function" ? json[key] : "\"" + json[key] + "\""));
		}
		buttons.push("\"" + list[i].value + "\": {" + array.join(",") + "}");
	}
	return "{" + buttons.join(",") + "}";
}
//获取操作信息
function getOpers() {
	var type = $("#type").val();
	if (type != "list") {
		return {};
	}
	var list = document.getElementsByName("pageOper");
	var opers = [];
	for (var i = 0;i < list.length;i ++) {
		var json = pageInitVar.listOper[list[i].value];
		var array = [];
		json.checked = list[i].checked;
		for (var key in json) {
			array.push("\"" + key + "\": " + (typeof json[key] == "function" ? json[key] : "\"" + json[key] + "\""));
		}
		opers.push("\"" + list[i].value + "\": {" + array.join(",") + "}");
	}
	return "{" + opers.join(",") + "}";
}
//获取单击信息
function getClick() {
	var type = $("#type").val();
	var list = document.getElementsByName("pageClick");
	var click = [];
	for (var i = 0;i < list.length;i ++) {
		var json = pageInitVar.clicks[list[i].value];
		var array = [];
		json.checked = list[i].checked;
		for (var key in json) {
			array.push("\"" + key + "\":" + (typeof json[key] == "function" ? json[key] : "\"" + json[key] + "\""));
		}
		click.push("\"" + list[i].value + "\":{" + array.join(",") + "}");
	}
	return "{" + click.join(",") + "}";
}
function addButton() {
	var type = $("#type").val();
	var text = $("#btnName").val();
	var fun;
	eval("fun=" + $("#btnContext").val());
	var successFun, successText = $("#successFun").val();
	if (successText) {
		eval("successFun=" + successText);
	} else {
		successFun = "";
	}
	//按钮
	if (btnType == 1) {
		var btn = {};
		if (type == "list" || type == "tree") {
			btn = pageInitVar.listButton;
		} else {
			btn = pageInitVar.editButton;
		}
		//判断是修改/新增按钮
		if (click_btn_id != null) {
			btn[click_btn_id].text = text;
			btn[click_btn_id].handler = fun;
			btn[click_btn_id].success = successFun;
		} else {
			var size = 0;
			for (var b in btn) {
				size ++;
			}
			btn["button_" + size] = {
				text: text,
				handler: fun,
				success: successFun
			};
			addPageButtonToPanel(btn["button_" + size], "button_" + size);
		}
	} else if (btnType == 2) {
		//判断是修改/新增按钮
		if (click_btn_id != null) {
			pageInitVar.listOper[click_btn_id].text = text;
			pageInitVar.listOper[click_btn_id].handler = fun;
			pageInitVar.listOper[click_btn_id].success = successFun;
		} else {
			var size = 0;
			for (var b in pageInitVar.listOper) {
				size ++;
			}
			pageInitVar.listOper["oper_" + size] = {
				text: text,
				handler: fun,
				success: successFun
			};
			addPageOperToPanel(pageInitVar.listOper["oper_" + size], "oper_" + size);
		}
	} else {
		pageInitVar.clicks[click_btn_id].text = text;
		pageInitVar.clicks[click_btn_id].handler = fun;
		pageInitVar.clicks[click_btn_id].success = successFun;
	}
	closeButWin();
}
//关闭按钮窗口
function closeButWin() {
	$('#buttonWindow').window('close');
}
var btnType = 0;
function openButWin(type){  //打开窗口
	btnType = type;
	click_btn_id = null;
	$("#btnName").val("");
	$("#btnContext").val("");
	$("#successFun").val("")
	$('#buttonWindow').window('open');
}